#!/bin/bash -e

set -x

export ENVIRONMENT=${ENVIRONMENT:-release}

tmpdir=$(mktemp -d)
trap "rm -rf $tmpdir" EXIT HUP TERM INT

# Put the enclave alongside the test binary so libadapters.so can find it
libs="$PWD/sgx/target/$ENVIRONMENT"
cp ./sgx/target/$ENVIRONMENT/enclave.signed.so $tmpdir/

for t in ./core/adapters/ ./core/services/attestation/; do
  executable="$(basename $t)"
  go test -tags=sgx_enclave -ldflags "-r $libs" $t -c -o "$tmpdir/$executable.test"

  pushd $tmpdir >/dev/null
  if [ -n "$USE_GDB" ] && [ "$ENVIRONMENT" == "debug" ]; then
    # XXX: The sgx-gdb wrapper script will actually load the symbol table for the enclave
    /opt/sgxsdk/bin/sgx-gdb -ex run --args ./$executable.test -test.parallel 1 -test.v
  else
    ./$executable.test -test.parallel 2 -test.v $@
  fi
  popd >/dev/null
done
